package com.yupi.mybi.bimq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 此类是处理消费者无法处理的消息（死信队列）
 */
@Configuration
//在开启消费者重试机制时候开启此配置类
public class WorkConfig {

    @Bean
    public DirectExchange workExchange() {
        return new DirectExchange(BiConstant.WORK_EXCHANGE);
    }

    @Bean
    public Queue workQueue(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", BiConstant.DLX_EXCHANGE);
        args.put("x-dead-letter-routing-key",BiConstant.DLX_ROUTINGKEY);
        return new Queue(BiConstant.WORK_QUEUE,true,false,false,args);
    }

    @Bean
    public Binding workBinding(Queue workQueue,DirectExchange workExchange){

        return BindingBuilder.bind(workQueue).to(workExchange).with(BiConstant.WORK_ROUTINGKEY);
    }
}
